package First;
import sun.plugin2.message.JavaObjectOpMessage;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
import First.Window;

import static com.sun.deploy.uitoolkit.ToolkitStore.dispose;
public class Login_admin {
    private JFrame frame=new JFrame("管理员登陆");
    private Container c=frame.getContentPane();
    private JTextField username=new JTextField();
    private JPasswordField password=new JPasswordField();

    private JButton ok=new JButton("确定");
    private JButton cancel=new JButton("取消");
    public Login_admin(){
        frame.setSize(300,200);
        c.setLayout(new BorderLayout());
        initFrame();
        frame.setVisible(true);
    }
    private void initFrame() {
        String a="123";
        //顶部
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new FlowLayout());
        titlePanel.add(new JLabel("系统管理员登录"));
        c.add(titlePanel,"North");
        //中部表单
        JPanel fieldPanel = new JPanel();
        fieldPanel.setLayout(null);
        JLabel l1 = new JLabel("工号/学号:");
        l1.setBounds(50, 20, 200, 20);
        JLabel l2 = new JLabel("密 码:");
        l2.setBounds(50, 60, 50, 20);
        fieldPanel.add(l1);
        fieldPanel.add(l2);
        username.setBounds(110,20,120,20);
        password.setBounds(110,60,120,20);
        fieldPanel.add(username);
        fieldPanel.add(password);
        c.add(fieldPanel,"Center");
        //底部按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(ok);
        buttonPanel.add(cancel);
        c.add(buttonPanel,"South");
        //按键监听
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection conn = null;
                Statement stat = null;
                ResultSet res = null;
                String passwd=new String(password.getPassword());//用户密码
                String userID=new String(username.getText());//用户ID号
                String sql = "SELECT Tno,Tname,Tpassword FROM Tpassword;";
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                } catch (Exception d){
                    System.out.println("jdbc fall");
                    d.printStackTrace();
                }
                try {
                    conn= DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mydata?serverTimezone=GMT%2B8",
                            "root",
                            "12345678");
                    stat=conn.createStatement();
                    res=stat.executeQuery(sql);
                    while (res.next()){
                        System.out.println(res.getString(1)+"%"+passwd+"%"+userID);
                        if (res.getString(1).equals(userID)){   //核对工号，确定是否有此人
                            if (res.getString(3).equals(passwd)){   //核对密码
                                Window window = new Window();
                                break;
                            }
                            else {
                                System.out.println("输入密码错误！");
                                JOptionPane.showMessageDialog(null, "输入密码错误！", "error", JOptionPane.ERROR_MESSAGE);
                                break;
                            }
                        }
                        else {
                            System.out.println("输入工号/学号错误！");
                            JOptionPane.showMessageDialog(null, "输入工号/学号错误！", "error", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }catch (SQLException el){
                    el.printStackTrace();
                }

            }
        });
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
    }
}
